package com.yule.runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * @author gjl
 * 2017/9/1
 */
@Component
@Order(value = 2)
public class RequestUrlRunner implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(RequestUrlRunner.class);

    private static Properties properties;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... arg0) throws Exception {
        properties = new Properties();
        InputStream in = null;
        //开发环境
        in = ClassLoader.class.getResourceAsStream("/requestUrl.properties");
        //生产环境读取配置文件
        //in = ClassLoader.class.getResourceAsStream("/BOOT-INF/classes/requestUrl.properties");
        properties.load(in);
        in.close();
        Set<?> keyValue = properties.keySet();
        for (Iterator<?> it = keyValue.iterator(); it.hasNext();) {
            String key = (String) it.next();
            String value = properties.getProperty(key);
            stringRedisTemplate.opsForValue().set("requestUrl_" + key.trim(), value.trim());
        }
        logger.info("============= 请求方法缓存加载完成 =============");
        //开发环境
        in = ClassLoader.class.getResourceAsStream("/requestPara.properties");
        //生产环境读取配置文件
        //in = ClassLoader.class.getResourceAsStream("/BOOT-INF/classes/requestPara.properties");
        properties.load(in);
        in.close();
        keyValue = properties.keySet();
        for (Iterator<?> it = keyValue.iterator(); it.hasNext();) {
            String key = (String) it.next();
            String value = properties.getProperty(key);
            stringRedisTemplate.opsForValue().set("requestPara_" + key.trim(), value.trim());
        }
        logger.info("============= 请求参数缓存加载完成 =============");
    }

}
